package com.two;

import cn.hutool.core.io.FileUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class RegisterJFrame extends JFrame implements MouseListener {
    private ArrayList<User> allUsers;
    //扩大三个输入框变量的作用范围
    private JTextField userNameTextField = new JTextField();
    private JPasswordField passwordTextField = new JPasswordField();
    private JTextField confirmPasswordTextField = new JTextField();
    private JButton registerButton = new JButton();
    private JButton reSetButton = new JButton();
    private JButton disappearPassword = new JButton();
    //设置密码不显示出来
    private boolean passwordVisible=false;
    public RegisterJFrame(ArrayList<User> allUsers){
        this.allUsers=allUsers;
        initJFrame();
        initContent();
        this.setVisible(true);
    }

    private void initContent() {
        //添加注册用户名的文本
        JLabel usernameText = new JLabel(new ImageIcon("image\\register\\注册用户名.png"));
        usernameText.setBounds(85, 135, 80, 20);
        //添加注册用户名的输入框
        userNameTextField.setBounds(195, 133, 200, 30);
        //添加注册密码的文本
        JLabel passwordText = new JLabel(new ImageIcon("image\\register\\注册密码.png"));
        passwordText.setBounds(97, 195, 70, 20);
        //添加密码输入框
        passwordTextField.setBounds(195, 192, 200, 30);
        passwordTextField.setEchoChar('*');
        disappearPassword.setIcon(new ImageIcon("image\\register\\隐藏密码.png"));
        disappearPassword.setBounds(397, 192, 30, 30);
        disappearPassword.setBorderPainted(false);
        disappearPassword.setContentAreaFilled(false);
        disappearPassword.addMouseListener(this);
        this.getContentPane().add(disappearPassword);
        //添加再次输入密码的文本
        JLabel confirmPasswordText = new JLabel(new ImageIcon("image\\register\\再次输入密码.png"));
        confirmPasswordText.setBounds(68, 255, 95, 20);
        //添加再次输入密码的输入框
        confirmPasswordTextField.setBounds(195, 253, 200, 30);
        //注册的按钮
        registerButton.setIcon(new ImageIcon("image\\register\\注册按钮.png"));
        registerButton.setBounds(123, 310, 128, 47);
        registerButton.setBorderPainted(false);
        registerButton.setContentAreaFilled(false);
        registerButton.addMouseListener(this);
        //重置的按钮
        reSetButton.setIcon(new ImageIcon("image\\register\\重置按钮.png"));
        reSetButton.setBounds(256, 310, 128, 47);
        reSetButton.setBorderPainted(false);
        reSetButton.setContentAreaFilled(false);
        reSetButton.addMouseListener(this);
        //背景图片
        JLabel background = new JLabel(new ImageIcon("image\\register\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(usernameText);
        this.getContentPane().add(passwordText);
        this.getContentPane().add(confirmPasswordText);
        this.getContentPane().add(userNameTextField);
        this.getContentPane().add(passwordTextField);
        this.getContentPane().add(confirmPasswordTextField);
        this.getContentPane().add(registerButton);
        this.getContentPane().add(reSetButton);
        this.getContentPane().add(background);
    }
    //创建一个弹框对象
    private JDialog jDialog=new JDialog();
    private void showJDialog(String content){
        if(!jDialog.isVisible()){
            //把弹框中原来的文字给清空
            jDialog.getContentPane().removeAll();
            //JLabel jLabel=new JLabel(content);
            JTextArea jTextArea=new JTextArea(content);
            jTextArea.setOpaque(false);
            //不可编辑
            jTextArea.setEditable(false);
            jTextArea.setBounds(0,0,200,150);
            jDialog.getContentPane().add(new JScrollPane(jTextArea));
            //jLabel.setBounds(0,0,200,150);
            //jDialog.add(jLabel);
            //给弹框设置大小
            jDialog.setSize(200,150);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            //设置应用图标
            try {
                jDialog.setIconImage(ImageIO.read(new File("image\\logo.png")));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            jDialog.setVisible(true);
        }
    }
    private void showDialogLable(String content){
        if(!jDialog.isVisible()){
            //把弹框中原来的文字给清空
            jDialog.getContentPane().removeAll();
            JLabel jLabel=new JLabel(content);
            jLabel.setBounds(0,0,200,150);
            jDialog.add(jLabel);
            //给弹框设置大小
            jDialog.setSize(200,150);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            //设置应用图标
            try {
                jDialog.setIconImage(ImageIO.read(new File("image\\logo.png")));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            jDialog.setVisible(true);
        }
    }
    /*
     * 作用：
     *       判断username在集合中是否存在
     * 参数：
     *       用户名
     * 返回值：
     *       true：存在
     *       false：不存在
     *
     * */
    //是否存在
    private boolean isExistUserName(String username){
        for (User users : allUsers) {
            if(users.getUsername().equals(username)){
                return true;
            }
        }
        return false;
    }
    private void initJFrame() {
        this.setSize(483,428);
        this.setTitle("拼图游戏 v1.0注册");
        //取消内部默认布局
        this.setLayout(null);
        //将注册页面设置为不能拖动
        this.setResizable(false);
        //设置应用图标
        try {
            this.setIconImage(ImageIO.read(new File("image\\logo.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置居中
        this.setLocationRelativeTo(null);
    }
    //设置观察密码密文的小眼睛

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==registerButton){
            //点击注册按钮
            //用户名，密码不能为空
            if(userNameTextField.getText().isEmpty()||passwordTextField.getText().isEmpty()||confirmPasswordTextField.getText().isEmpty()){
                showDialogLable("           用户名和密码不能为空");
                return;
            }
            //判断两次输入密码是否一致
            if(!passwordTextField.getText().equals(confirmPasswordTextField.getText())){
                showDialogLable("           两次输入密码不一致");
                return;
            }
            /*用户名要求：
            1.用户名由4到16个字符组成。
            2.字符可以是大小写字母（a-z, A-Z）和数字（0-9）
            */
            //判断用户名和密码的格式是否正确
            if(!userNameTextField.getText().matches("[a-zA-Z0-9]{4,16}")){
                showDialogLable("           用户名格式不正确");
                return;
            }
            /*密码要求：
            1.包含至少6个字符
            2.包含至少一个数字
            3.包含至少一个大写字母
            4.包含至少一个小写字母
            5.包含至少一个特殊字符（!@#$%^&*?）
            6.密码长度不能超过20个字符
            */
            if(!passwordTextField.getText().matches("\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*[!@#$%^&*? ])\\S*")){
                showJDialog("              密码格式不正确\n至少包含1个小写字母\n1个大写字母\n1个特殊字符\n1个数字\n长度要求至少6位最多20位");
                return;
            }
            //判断用户名是否重复
            if(isExistUserName(userNameTextField.getText())){
                showJDialog("           用户名已存在\n请重新设置您的用户名");
                return;
            }
            //添加用户
            allUsers.add(new User(userNameTextField.getText(),passwordTextField.getText()));
            //写入文件-->设置集合对象，设置文件路径，设置文件编码格式
            //FileUtil.writeLines(allUsers,"D:\\JavaprojectIdea\\puzzlegame\\userinfo.txt","UTF-8");
            //设置写入文件的相对路径
            FileUtil.writeLines(allUsers,"userinfo.txt","UTF-8");
            
            //提示注册成功
            showDialogLable("           恭喜您！注册成功");
            this.setVisible(false);
            new LoginJFrame();
        } else if(e.getSource()==reSetButton){
            //重置三个输入框
            userNameTextField.setText("");
            passwordTextField.setText("");
            confirmPasswordTextField.setText("");
        }else if(e.getSource()==disappearPassword){
            //显示密码
            //passwordTextField.setEchoChar((char)0);
            //图片由隐藏变为显示
            //disappearPassword.setIcon(new ImageIcon("image\\register\\显示密码.png"));
            if (passwordVisible) {
                passwordTextField.setEchoChar('*');
                //切换为显示密码的图标
                disappearPassword.setIcon(new ImageIcon("image\\register\\隐藏密码.png"));
            } else {
                //切换为隐藏密码的图标
                passwordTextField.setEchoChar((char) 0);
                disappearPassword.setIcon(new ImageIcon("image\\register\\显示密码.png"));
            }
            //切换密码显示状态
            passwordVisible = !passwordVisible;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == registerButton) {
            registerButton.setIcon(new ImageIcon("image\\register\\注册按下.png"));
        } else if (e.getSource() == reSetButton) {
            reSetButton.setIcon(new ImageIcon("image\\register\\重置按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == registerButton) {
            registerButton.setIcon(new ImageIcon("image\\register\\注册按钮.png"));
        } else if (e.getSource() == reSetButton) {
            reSetButton.setIcon(new ImageIcon("image\\register\\重置按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
